//
//  MyLoopsView.m
//  MyLoops
//
//  Created by dllo on 16/1/16.
//  Copyright © 2016年 Let_It_Be. All rights reserved.
//

#import "MyLoopsView.h"

#define VScreen_Width self.frame.size.width
#define VScreen_Height self.frame.size.height

@interface MyLoopsView ()<UIScrollViewDelegate>
@property(nonatomic, retain) NSTimer *timer;
@property(nonatomic, retain) NSArray *imageArr;
@property(nonatomic, retain) UIScrollView *scrollView;
@property(nonatomic, retain) UIImageView *leftImageView;
@property(nonatomic, retain) UIImageView *centerImageView;
@property(nonatomic, retain) UIImageView *rightImageView;
@property(nonatomic, assign) NSInteger currentIndex;
@property(nonatomic, retain) UIPageControl *page;
@end

@implementation MyLoopsView
#pragma mark Dealloc & Init
- (void)dealloc
{
    [self removeObserver:self forKeyPath:@"isAutoPlay" context:nil];
    Block_release(_block);
    [_timer release];
    [_imageArr release];
    [_scrollView release];
    [_leftImageView release];
    [_centerImageView release];
    [_rightImageView release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame imageArr:(NSArray *)imageArr {
    self = [super initWithFrame:frame];
    if (self) {
        [self addObserver:self forKeyPath:@"isAutoPlay" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
        self.imageArr = imageArr;
        [self creatScrollView];
        [self creatImageView];
        [self creatPage];
        self.isAutoPlay = YES;
    }
    return self;
}

+ (instancetype)myLoopsViewWithFrame:(CGRect)frame imageArr:(NSArray *)imageArr {
    return [[[MyLoopsView alloc] initWithFrame:frame imageArr:imageArr] autorelease];
}

#pragma mark 观察BOOL值
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    BOOL isPlay = [change[@"new"] boolValue];
    if (isPlay) {
        [self openTimer];
    } else {
        [self closeTimer];
    }
}

#pragma mark 创建视图控件
- (void)creatScrollView {
    self.scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    [self addSubview:self.scrollView];
    [_scrollView release];
    self.scrollView.delegate = self;
    self.scrollView.contentSize = CGSizeMake(VScreen_Width * 3, 0);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.currentIndex = 0;
    self.scrollView.contentOffset = CGPointMake(VScreen_Width, 0);
}

- (void)creatImageView {
    //
    self.leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, VScreen_Width, VScreen_Height)];
    self.centerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(VScreen_Width, 0, VScreen_Width, VScreen_Height)];
    self.rightImageView = [[UIImageView alloc] initWithFrame:CGRectMake(VScreen_Width * 2, 0, VScreen_Width, VScreen_Height)];
    [self.scrollView addSubview:self.leftImageView];
    [self.scrollView addSubview:self.centerImageView];
    [self.scrollView addSubview:self.rightImageView];
    
    [_leftImageView release];
    [_centerImageView release];
    [_rightImageView release];
    self.centerImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [self.centerImageView addGestureRecognizer:tap];
    [tap release];
    [self changeThreeImage:self.imageArr.count - 1 centerNum:0 rightNum:1];
}

- (void)creatPage {
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(VScreen_Width / 2, VScreen_Height - 10, VScreen_Width / 2, 7)];
    [self addSubview:self.page];
    [_page release];
    self.page.numberOfPages = self.imageArr.count;
    self.page.currentPage = 0;
    self.page.currentPageIndicatorTintColor = [UIColor whiteColor];
    self.page.pageIndicatorTintColor = [UIColor lightGrayColor];
}

#pragma mark 点击触发block
- (void)tapAction:(UITapGestureRecognizer *)tap
{
    if (self.block != nil) {
        self.block(self.currentIndex);
    }
}

#pragma mark 开启计时器
- (void)openTimer {
    if (!self.timer) {
        self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(changeImage:) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    }
}

#pragma mark 关闭计时器
- (void)closeTimer {
    if (self.timer) {
        [self.timer invalidate];
        self.timer = nil;
    }
}

- (void)changeImage:(NSTimer *)timer {
    [self.scrollView setContentOffset:CGPointMake(self.scrollView.contentOffset.x + VScreen_Width, 0) animated:YES];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    self.isAutoPlay = NO;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    self.isAutoPlay = YES;
}

#pragma mark 图片的切换处理
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat offSetX = scrollView.contentOffset.x;
    if (offSetX >= VScreen_Width * 2) {
        self.currentIndex++;
        if (self.currentIndex == self.imageArr.count - 1) {
            [self changeThreeImage:self.currentIndex - 1 centerNum:self.currentIndex rightNum:0];
        } else if (self.currentIndex == self.imageArr.count) {
            self.currentIndex = 0;
            [self changeThreeImage:self.imageArr.count - 1 centerNum:0 rightNum:1];
        } else {
            [self changeThreeImage:self.currentIndex - 1 centerNum:self.currentIndex rightNum:self.currentIndex + 1];
        }
    }
    if (offSetX <= 0) {
        self.currentIndex--;
        if (self.currentIndex == 0) {
            [self changeThreeImage:self.imageArr.count - 1 centerNum:0 rightNum:1];
        } else if (self.currentIndex == -1) {
            self.currentIndex = self.imageArr.count - 1;
            [self changeThreeImage:self.currentIndex - 1 centerNum:self.currentIndex rightNum:0];
        } else {
            [self changeThreeImage:self.currentIndex - 1 centerNum:self.currentIndex rightNum:self.currentIndex + 1];
        }
    }
    self.page.currentPage = self.currentIndex;
}

- (void)changeThreeImage:(NSInteger)leftNum centerNum:(NSInteger)centerNum rightNum:(NSInteger)rightNum {
    //  获取
//    self.leftImageView.image = [UIImage imageNamed:self.imageArr[leftNum]];
//    self.centerImageView.image = [UIImage imageNamed:self.imageArr[centerNum]];
//    self.rightImageView.image = [UIImage imageNamed:self.imageArr[rightNum]];
    [self.leftImageView sd_setImageWithURL:self.imageArr[leftNum]placeholderImage:[UIImage imageNamed:@"zhanweitu.jpg"]];
    [self.centerImageView sd_setImageWithURL:self.imageArr[centerNum]placeholderImage:[UIImage imageNamed:@"zhanweitu.jpg"]];
    [self.rightImageView sd_setImageWithURL:self.imageArr[rightNum]placeholderImage:[UIImage imageNamed:@"zhanweitu.jpg"]];
    self.scrollView.contentOffset = CGPointMake(VScreen_Width, 0);
}




@end
